/*
 * Scoresheet class to hold multiple high scores
 * @author Andrew cook
 */
package vooga.core;



public class ScoreSheet 
{
	private HighScore[] myScores;
	private vooga.util.PropertiesManager myProperties;
	private String myFilename;
	private HighScore tempScore;
	private long tempNum;
	private String tempName;
	
	public ScoreSheet(String gameName)
	{
		myFilename = gameName + "-highScores";
		myScores = new HighScore[10];
		populateList();
	}
	
	public void populateList()
	{
		try
		{
			myProperties = new vooga.util.PropertiesManager(myFilename);
			for(int n = 0; n<10; n++)
			{
				tempName = myProperties.getString("name" + n);
				if(tempName==null)
				{
					tempName = "blank";
					tempNum = 10-n;
				}
				else
				{
					tempNum = Long.parseLong(myProperties.getString("score" + n));
				}
				myScores[n] = new HighScore(tempName, tempNum);
			}
		}
		catch(java.io.IOException e)
		{
			System.out.println("IO Exception");
		}
		writeScores();
	}
	
	public void writeScores()
	{
		for(int n = 0; n<10; n++)
		{
			try
			{
				myProperties.setProperty("name" + n, myScores[n].getName());
				myProperties.setProperty("score" + n, new Long(myScores[n].getScore()).toString());
			}
			catch(java.io.IOException e)
			{
				System.out.println("IO Exception iteration" + n);
			}
		}
	}
	
	public void printList(Layer layer)
	{
		//TODO: Code to print high scores on splash screen
	}
	
	public void addScore(HighScore newScore)
	{
		for(int x = 0; x<10; x++)
		{
			if(newScore.isLarger(myScores[x]))
			{
				tempScore = myScores[x];
				myScores[x] = newScore;
				addScore(tempScore);
			}
		}
		writeScores();
	}
}
